import 'package:flutter/material.dart';
import 'package:practice_flutter/pages/NotFound.dart';
import 'package:practice_flutter/pages/house/build_list.dart';
import 'package:practice_flutter/pages/house/house_detail.dart';
import 'package:practice_flutter/pages/house/house_form.dart';
import 'package:practice_flutter/pages/house/house_list.dart';
import 'package:practice_flutter/pages/house/location_list.dart';
import 'package:practice_flutter/pages/house/room_list.dart';
import 'package:practice_flutter/pages/login/index.dart';
import 'package:practice_flutter/pages/notice/index.dart';
import 'package:practice_flutter/pages/profile/index.dart';
import 'package:practice_flutter/pages/tabbar_page.dart';
import 'package:practice_flutter/utils/token.dart';

class RouterManger {
  static Map<String, WidgetBuilder> routers = {
    '/': (context) => const TabBarPage(),
    '/detail': (context) => const NoticeDetail(),
    '/login': (context) => const LoginPage(),
  };
  static Route<dynamic> onGenerateRoute(RouteSettings settings) {
    final tokenManager = TokenManager();
    final token = tokenManager.getToken() ?? '';
    if (token.isEmpty && settings.name != '/login') {
      return MaterialPageRoute(builder: (context) => const LoginPage());
    }

    if (settings.name == '/profile') {
      return MaterialPageRoute(
          builder: (context) =>
              ProfilePage(userInfo: settings.arguments as Map));
    }

    if (settings.name == '/house_list') {
      return MaterialPageRoute(builder: (context) => const HouseList());
    }
    if (settings.name == '/Location_list') {
      return MaterialPageRoute(builder: (context) => const LocationList());
    }
    if (settings.name == '/build_list') {
      return MaterialPageRoute(
          builder: (context) =>
              BuildingList(point: (settings.arguments as Map)['point']));
    }
    if (settings.name == '/room_list') {
      Map temp = settings.arguments as Map;
      return MaterialPageRoute(
          builder: (context) => RoomList(
                point: temp['point'],
                building: temp['building'],
              ));
    }
    if (settings.name == '/house_form') {
      return MaterialPageRoute(
          builder: (context) => HouseForm(
                params: settings.arguments as Map,
              ));
    }
    if (settings.name == '/house_detail') {
      return MaterialPageRoute(
          builder: (context) =>
              HouseDetail(id: (settings.arguments as Map)['id']));
    }
    return MaterialPageRoute(builder: (context) => const Notfound());
  }
}
